<?php


namespace seecms\lib\builder\control;

use seecms\See;

class FileControl extends Control
{
    protected $type = 'file';

    /**
     * 默认的扩展文件
     * @var array
     */
    protected $extension = ['xls', 'xlsx', 'csv', 'txt', 'doc', 'docx', 'ppt', 'pptx', 'pdf', 'jpg', 'png', 'gif', 'jpeg'];

    /**
     * 上传数量
     * @var int
     */
    protected $limit = 1;

    /**
     * 上传文件大小 默认2M
     * @var int
     */
    protected $size = 2097152;

    /**
     * 是否多选
     * @var bool
     */
    protected $multiple = false;

    /**
     * @var string
     */
    protected $accept = 'file';

    /**
     * 磁盘
     * @var string
     */
    protected $disk = '';

    /**
     * 上传路径
     * @var string
     */
    protected $url = '';

    /**
     * 允许从附件中选择
     * @var bool
     */
    protected $choose = true;

    /**
     * @return array
     */
    public function getExtension(): array
    {
        return $this->extension;
    }

    /**
     * @param array $extension
     * @return FileControl
     */
    public function setExtension(array $extension): FileControl
    {
        $this->extension = $extension;

        return $this;
    }

    /**
     * @return int
     */
    public function getLimit(): int
    {
        return $this->limit;
    }

    /**
     * @param int $limit
     * @return FileControl
     */
    public function setLimit(int $limit): FileControl
    {
        $this->limit = $limit;

        return $this;
    }

    /**
     * @return int
     */
    public function getSize(): int
    {
        return $this->size;
    }

    /**
     * @param int $size
     * @return $this
     */
    public function setSize(int $size): FileControl
    {
        $this->size = $size;

        return $this;
    }

    public function getAccept(): string
    {
        return $this->accept;
    }

    /**
     * @param string $accept
     * @return $this
     */
    public function setAccept(string $accept): FileControl
    {
        $this->accept = $accept;

        return $this;
    }

    public function getDisk(): string
    {
        return $this->disk ?: '';
    }

    /**
     * @param string $disk
     * @return $this
     */
    public function setDisk(string $disk): FileControl
    {
        $this->disk = $disk;

        return $this;
    }

    public function getUrl(): string
    {
        return $this->url ?: See::config()->upload->url();
    }

    public function setUrl(string $url): FileControl
    {
        $this->url = $url;

        return $this;
    }

    public function isChoose(): bool
    {
        return $this->choose;
    }

    public function setChoose(bool $choose = false): FileControl
    {
        $this->choose = $choose;

        return $this;
    }

    public function render(): string
    {

        $required = $this->required ? '<span class="layui-font-red">*</span>' : '';

        $disabled = $this->disabled ? 'disabled' : '';
        $description = $this->getDescription();

        $extension = implode('|', $this->extension);
        $url = $this->url ?: See::config()->upload->url();

        if ($disabled) {
            $disabled = 'type="button" class="layui-btn layui-btn-normal layui-btn-disabled"';
        } else {
            $disabled = <<<EOF
                        type="button" class="layui-btn layui-btn-normal layui-btn-sm"
                        id="upload-{$this->name}"
                        data-name="{$this->name}"
                        data-limit="{$this->limit}"
                        data-size="{$this->size}}"
                        data-ext="{$extension}"
                        data-multiple="{$this->multiple}"
                        data-disk="{$this->disk}"
                        data-url="{$url}"
                        data-upload
EOF;
        }

        $accept = __('upload.file');
        if ($this->accept === 'images') {
            $accept = __('upload.image');
        }

        $choose = '';
        if ($this->choose && !$this->readonly) {
            $text = __('upload.choose');
            $choose = <<<EOF
                    <button type="button" class="layui-btn layui-btn-normal layui-btn-sm"
                        data-name="{$this->name}"
                        data-limit="{$this->limit}"
                        data-ext="{$extension}"
                        data-choose-attachment>
                    <i class="iconfont icon-other"></i>
                    $text
                </button>
EOF;
        }

        $word_aux = '';
        if (!$this->readonly) {
            $limit_num_tip = __('upload.limit_num_tip') . ':' . $this->limit;
            $limit_size_tip = __('upload.limit_size_tip') . ':' . ($this->size / 1024) . 'k';
            $limit_ext_tip = __('upload.limit_ext_tip') . ':' . implode(',', $this->extension);;
            $word_aux = <<<EOF
 <div class="layui-word-aux">
                $limit_num_tip,
                $limit_size_tip,
                $limit_ext_tip
            </div>
EOF;
        }

        $values = $this->value;
        if (is_array($values)) {
            $values = implode(',', $values);
        }

        $display = 'block';
        $container = '<div class="layui-form-item">';
        if ($this->inline == 'inline') {
            $display = 'inline';
            $container = '<div class="layui-inline">';
        }


        return <<<EOF
$container
    <label class="layui-form-label">
        {$this->label}
        {$required}
    </label>
    <div class="layui-input-$display">
        <div>
            <button {$disabled}>
                <i class="layui-icon">&#xe67c;</i>
                $accept
            </button>
            $choose
        </div>

        <ul class="layui-row list-inline plupload-preview"
            data-limit="{$this->limit}"
            data-name="{$this->name}"
            id="upload_thumb_water_pic_{$this->name}"
            data-upload-values="{$values}"
        >
        </ul>
        $word_aux
        $description
    </div>
</div>
EOF;

    }
}